package net.caixiaomi.info.ui.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ElementCellAdapter;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.widgets.GridDivider;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 比分选择dialog
 */

public class ScoreDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.host)
    TextView mHost;
    @BindView(R.id.guest)
    TextView mGuest;

    @BindView(R.id.win)
    TextView mWin;
    @BindView(R.id.draw)
    TextView mDraw;
    @BindView(R.id.defeat)
    TextView mDefeat;
    @BindView(R.id.list1)
    RecyclerView mListView1;
    @BindView(R.id.list2)
    RecyclerView mListView2;
    @BindView(R.id.list3)
    RecyclerView mListView3;

    private PlayFootballItem mPlayFootBallItem;
    private ElementCellAdapter mAdapter1;
    private ElementCellAdapter mAdapter2;
    private ElementCellAdapter mAdapter3;

    @OnClick(android.R.id.button1)
    void toConfirm() {
        try {
            String text = JSON.toJSONString(mPlayFootBallItem);
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_FOOTBALL_SELECT_DONE, text));
            dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(android.R.id.button2)
    void toCancel() {
        dismiss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_scroe;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            String strData = getArguments().getString("data");
            mPlayFootBallItem = JSON.parseObject(strData, new TypeReference<PlayFootballItem>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        try {
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            wlp.gravity = Gravity.BOTTOM;
            window.setAttributes(wlp);
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setBackgroundDrawable(new ColorDrawable(0xff000000));
            getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);

            mHost.setText(mPlayFootBallItem.getHomeTeamAbbr());
            mGuest.setText(mPlayFootBallItem.getVisitingTeamAbbr());
            mWin.setText(R.string.C_WIN);
            mWin.setBackgroundColor(Color.parseColor("#EFAB50"));
            mDraw.setText(R.string.C_DRAW);
            mDraw.setBackgroundColor(Color.parseColor("#69ADDA"));
            mDefeat.setText(R.string.C_DEFEAT);
            mDefeat.setBackgroundColor(Color.parseColor("#88C172"));

            MatchPlaysList matchPlaysList = mPlayFootBallItem.getMatchPlays().get(0);

            if (mPlayFootBallItem.getSelectCell() != null) {
                mPlayFootBallItem.getSelectCell().clear();
            }
            List<ElementCellModel> winCellSons = matchPlaysList.getHomeCell().getCellSons();
            for (ElementCellModel cellModel : winCellSons) {
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            List<ElementCellModel> drawCellSons = matchPlaysList.getFlatCell().getCellSons();
            for (ElementCellModel cellModel : drawCellSons) {
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            List<ElementCellModel> defeatCellSons = matchPlaysList.getVisitingCell().getCellSons();
            for (ElementCellModel cellModel : defeatCellSons) {
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }

            mListView1.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            mListView1.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView1.setNestedScrollingEnabled(false);
            mAdapter1 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter1.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
                @Override
                public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                    if (position == mAdapter1.getData().size() - 1)
                        return 3;
                    return 1;
                }
            });
            mAdapter1.setOnItemClickListener(this);
            mAdapter1.setNewData(winCellSons);
            mListView1.setAdapter(mAdapter1);
            mAdapter1.bindToRecyclerView(mListView1);

            mListView2.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            mListView2.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider), ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView2.setNestedScrollingEnabled(false);
            mAdapter2 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter2.setOnItemClickListener(this);
            mAdapter2.setNewData(drawCellSons);
            mAdapter2.bindToRecyclerView(mListView2);

            mListView3.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            mListView3.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView3.setNestedScrollingEnabled(false);
            mAdapter3 = new ElementCellAdapter(R.layout.view_textview);
            mAdapter3.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
                @Override
                public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                    if (position == mAdapter3.getData().size() - 1)
                        return 3;
                    return 1;
                }
            });
            mAdapter3.setOnItemClickListener(this);
            mAdapter3.setNewData(defeatCellSons);
            mListView3.setAdapter(mAdapter3);
            mAdapter3.bindToRecyclerView(mListView3);

//            View parent = (View) view.getParent();
//            BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
//            view.measure(0, 0);
//            behavior.setPeekHeight(view.getMeasuredHeight());

            getDialog().setCanceledOnTouchOutside(true);
            getDialog().setCancelable(true);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            if (mPlayFootBallItem.getSelectCell() == null) {
                mPlayFootBallItem.setSelectCell(new ArrayList<ElementCellModel>());
            }
            ElementCellModel cellModel = (ElementCellModel) adapter.getData().get(position);
            cellModel.setPlayType(mPlayFootBallItem.getPlayType());
            cellModel.setSelected(!cellModel.isSelected());
            if (cellModel.isSelected()) {
                mPlayFootBallItem.getSelectCell().add(cellModel);
            } else {
                mPlayFootBallItem.getSelectCell().remove(cellModel);
            }
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPlayFootBallItem = null;
    }
}
